const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadText: "",
    serviceData: [], //申请
    isHints: false,
    orderCode: '',
    shopName: '',
  },
  onLoad: function (options) {
    this.setData({
      orderCode: options.orderCode,
      shopName: options.shopName
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    this.setData({
      loadText: "",
      serviceData: [],
      isHints: false,
    }, () => {
      this.getServiceList()
    })
  },

  /**
   * 售后列表
   */
  getServiceList: function () {
    let that = this;
    var param = {
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      orderCode: that.data.orderCode, //订单号
    }
    giftShopModel.orderRefundGood(param).then(data => {
      if (data && data.length > 0) {
        that.setData({
          isLoadData: false,
          isHints: false,
          serviceData: data
        })
      } else {
        that.setData({
          loadText: "",
          isLoadData: false,
          isHints: true,
          serviceData: []
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 取消换货
   */
  cancelService(e) {
    wx.reportAnalytics('gift_service_cancelapply');
    let that = this;
    let code = e.currentTarget.dataset.code //订单号
    wx.showModal({
      title: '是否取消换货申请？',
      cancelColor: '#333333', //取消文字的颜色
      confirmColor: '#333333', //确定文字的颜色
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          giftShopModel.serviceOrderCancel({
            merchantId: wx.getStorageSync('merchantId'), //商户ID
            serviceOrderCode: code, //服务单号
          }).then(data => {
            that.setData({
              serviceData: [], //申请
              isHints: false,
              pageCount: 0,
              pageIndex: 1,
              pageCount: 0,
              loadText: "",
            }, () => {
              that.searchServiceOrder();
            })
          }).catch(err => {
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {}
      }
    })
  },


  /**
   * 跳转到服务单详情页面
   */
  goServiceDetail: function (e) {
    let code = e.currentTarget.dataset.code //服务单号
    if (code) {
      wx.reportAnalytics('gift_service_detail');
      wx.navigateTo({
        url: '/pages/giftShop/serviceOrder/serviceDetails/index?serviceOrderCode=' + code
      })
    }
  },

  /**
   * 换货
   */
  setAfterSale: function (e) {
    wx.reportAnalytics('gift_service_apply');
    let partnerSign = e.currentTarget.dataset.id
    let skuId = e.currentTarget.dataset.sku
    let quantity = e.currentTarget.dataset.num
    let priceType = e.currentTarget.dataset.type
    giftShopModel.serviceOrderPreCheck({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      orderCode: this.data.orderCode, //订单号
      partnerSign: partnerSign, //第三方id
      skuId: skuId, //商品sku
      quantity: quantity, //换货数量
      priceType: priceType // 价格类型 1积分 2飞豆 （商品详细数据对象中返回）
    }).then(data => {
      console.log(encodeURIComponent(JSON.stringify(data)))
      wx.navigateTo({
        url: `/pages/giftShop/serviceOrder/afterType/index?currency=${encodeURIComponent(JSON.stringify(data))}`
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  }
})